package com.scau.ums.service.imp.RepairReport;

import com.github.pagehelper.PageHelper;
import com.scau.ums.mapper.CustomedRepairReportMapper;
import com.scau.ums.mapper.RepairreportMapper;
import com.scau.ums.model.Repairreport;
import com.scau.ums.model.RepairreportExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RepairReportImp {
    @Autowired
    private CustomedRepairReportMapper customedRepairReportMapper;
    @Autowired
    private RepairreportMapper repairReportMapper;
    private Map<String, Integer> stuAnalysisMap = new HashMap<>();
    private Map<String, Integer> teaAnalysisMap = new HashMap<>();
    final static int repairTypeNum=4;

    public List<Repairreport> getAllUnOrderTaskRepairReports()
    {
        RepairreportExample example = new RepairreportExample();
        example.createCriteria().andStateEqualTo(2);
        return repairReportMapper.selectByExample(example);
    }

    public List<Repairreport> getUnverifiedRepairReports(Integer currentPage){
        PageHelper.startPage(currentPage,5);
        return customedRepairReportMapper.getUnverifiedRepairReports();
    }


    public int VerifySuccess(Integer RepairReportId){
        return customedRepairReportMapper.VerifySuccess(RepairReportId);
    }

    public int VerifyFailed(Integer RepairReportId,String remark){
        return customedRepairReportMapper.VerifyFailed(RepairReportId,remark);
    }

    public List<Repairreport> getAllFinishedReport(Integer currentPage){
        PageHelper.startPage(currentPage,5);
        return customedRepairReportMapper.getAllFinishedReport();
    }

    public List<Repairreport> getFinishedReportsOfMonth(){
        List<Integer> FinishedReportIdsOfMonth=customedRepairReportMapper.getFinishedReportIdsOfMonth();
        List<Repairreport> FinishedReportsOfMonth = new ArrayList<Repairreport>();
        for(int i=0;i<FinishedReportIdsOfMonth.size();i++){
            int index = FinishedReportIdsOfMonth.get(i);
            Repairreport repairReport = repairReportMapper.selectByPrimaryKey(index);
            System.out.println(repairReport);
            FinishedReportsOfMonth.add(repairReport);
        }
        return FinishedReportsOfMonth;
    }

    public List<Repairreport> getFinishedReportsOfSeason(){
        List<Integer> FinishedReportIdsOfSeason = customedRepairReportMapper.getFinishedReportIdsOfSeason();
        List<Repairreport> FinishedReportsOfSeason = new ArrayList<Repairreport>();
        for(int i=0;i<FinishedReportIdsOfSeason.size();i++){
            int index = FinishedReportIdsOfSeason.get(i);
            Repairreport repairReport = repairReportMapper.selectByPrimaryKey(index);
            FinishedReportsOfSeason.add(repairReport);
        }
        return FinishedReportsOfSeason;
    }

    public List<Repairreport> getFinishedReportsOfYear(){
        List<Integer> FinishedReportIdsOfYear = customedRepairReportMapper.getFinishedReportIdsOfYear();
        List<Repairreport> FinishedReportsOfYear = new ArrayList<Repairreport>();
        for(int i=0;i<FinishedReportIdsOfYear.size();i++){
            int index = FinishedReportIdsOfYear.get(i);
            Repairreport repairReport = repairReportMapper.selectByPrimaryKey(index);
            FinishedReportsOfYear.add(repairReport);
        }
        return FinishedReportsOfYear;
    }

    public List<Repairreport> getCustomedFinishedReports(String periodType,String ReportType,String reporterType){
        List<Repairreport> CustomedFinishedReports=new ArrayList<Repairreport>();
        CustomedFinishedReports=getFinishedReportsOfPeriodType(periodType);
        CustomedFinishedReports=getFinishedReportsOfReportType(CustomedFinishedReports,ReportType);
        CustomedFinishedReports=getFinishedReportsOfReporterType(CustomedFinishedReports,reporterType);
        return CustomedFinishedReports;
    }

    public List<Repairreport> getFinishedReportsOfPeriodType(String periodType){
        List<Repairreport> FinishedReportsOfPeriodType=new ArrayList<Repairreport>();
        if(periodType.equals("0"))
            return repairReportMapper.selectByExample(null);
        switch (periodType){
            case "1":FinishedReportsOfPeriodType=getFinishedReportsOfMonth();
                    break;
            case "2":FinishedReportsOfPeriodType=getFinishedReportsOfSeason();
                    break;
            case "3":FinishedReportsOfPeriodType=getFinishedReportsOfYear();
                    break;
        }
        return FinishedReportsOfPeriodType;
    }

    public List<Repairreport> getFinishedReportsOfReportType(List<Repairreport> FinishedReportsOfPeriodType,String ReportType){
        List<Repairreport> FinishedReportsOfReportType=new ArrayList<Repairreport>();
        if(ReportType.equals("0"))
            return FinishedReportsOfPeriodType;
        for(int i=0;i<FinishedReportsOfPeriodType.size();i++){
            Repairreport repairReport=FinishedReportsOfPeriodType.get(i);

            if(String.valueOf(repairReport.getType()).equals(ReportType)){
                FinishedReportsOfReportType.add(repairReport);
            }
        }
        return FinishedReportsOfReportType;
    }

    public List<Repairreport> getFinishedReportsOfReporterType(List<Repairreport> FinishedReportsOfReportType,String reporterType){
        List<Repairreport> FinishedReportsOfReporterType=new ArrayList<Repairreport>();
        if(reporterType.equals("0"))
            return FinishedReportsOfReportType;
        for(int i=0;i<FinishedReportsOfReportType.size();i++){
            Repairreport repairReport= FinishedReportsOfReportType.get(i);
            if(String.valueOf(repairReport.getReportertype()).equals(reporterType))
                FinishedReportsOfReporterType.add(repairReport);
        }
        return FinishedReportsOfReporterType;
    }

    public Map<String,Integer> getStuAnalysisMap(){
        for(int type=1;type<=repairTypeNum;type++){
            int num=customedRepairReportMapper.getStuRepairReportTypeNum(type);
            stuAnalysisMap.put("stu"+type,num);
        }
        return stuAnalysisMap;
    }

    public Map<String,Integer> getTeaAnalysisMap(){
        for(int type=1;type<=repairTypeNum;type++){
            int num=customedRepairReportMapper.getTeaRepairReportTypeNum(type);
            teaAnalysisMap.put("tea"+type,num);
        }
        return teaAnalysisMap;
    }

    public int getLikeNumber(){
        return customedRepairReportMapper.getLikeNumber();
    }

    public int getUnLikeNumber(){
        return customedRepairReportMapper.getUnLikeNumber();
    }

    public int getSomeoneLikeNumber(Integer maintainerId){
        return customedRepairReportMapper.getSomeoneLikeNumber(maintainerId);
    }

    public int getSomeoneUnlikeNumber(Integer maintainerId){
        return customedRepairReportMapper.getSomeoneUnLikeNumber(maintainerId);
    }
}
